package String;

import org.junit.Test;

public class _680_ValidPalindromeII {
    public boolean validPalindrome(String s) {
        int start = 0, end = s.length() - 1;
        boolean delete = false;
        while (start < end) {
            if (s.charAt(start) == s.charAt(end)) {
                start++;
                end--;
            } else {
                if (delete) {
                    return false;
                } else {
                    String middle1 = s.substring(0, start) + s.substring(start + 1, s.length());
                    String middle2 = s.substring(0, end) + s.substring(end + 1, s.length());
                    if (middle1.equals(new StringBuffer(middle1).reverse().toString())) {
                        delete = true;
                        start += 2;
                        end--;
                    } else if (middle2.equals(new StringBuffer(middle2).reverse().toString())) {
                        delete = true;
                        end -= 2;
                        start++;
                    } else {
                        return false;
                    }
                    //couldn't deal with the situation like 'abcudefgfeducuba'
                    /*if (s.charAt(start + 1) == s.charAt(end)) {
                        delete = true;
                        start += 2;
                        end--;
                    } else if (s.charAt(start) == s.charAt(end - 1)) {
                        delete = true;
                        end -= 2;
                        start++;
                    } else {
                        return false;
                    }*/
                }
            }
        }
        return true;
    }

    public void test() {
        String s = "aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuclmgmqfvnbgtapekouga";
        System.out.println(new StringBuffer(s).reverse().toString());
        System.out.println(s.equals(new StringBuffer(s).reverse().toString()));
    }
}
